package com.ptszyxx.popose.common.utils;

import android.util.Log;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.module.chat.config.GenerateTestUserSig;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMConversationListener;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMUserFullInfo;
import com.tencent.imsdk.v2.V2TIMValueCallback;
import com.tencent.liteav.trtccalling.zhiyu.CallActivity;
import com.tencent.liteav.trtccalling.zhiyu.enums.CallFreeTypeEnum;
import com.tencent.qcloud.tuicore.interfaces.TUILoginListener;
import com.ysg.base.BaseApplication;
import com.ysg.enums.ChatCallEnum;
import com.ysg.http.data.entity.user.UserResult;
import com.tencent.qcloud.tuicore.TUILogin;
import com.tencent.qcloud.tuicore.interfaces.TUICallback;
import com.tencent.qcloud.tuicore.util.ToastUtil;
import com.ptszyxx.popose.MyApp;
import com.ptszyxx.popose.module.chat.entity.UserInfo;
import com.ptszyxx.popose.module.chat.config.AppConfig;
import com.ptszyxx.popose.module.chat.service.TIMAppService;
import com.ptszyxx.popose.module.chat.utils.TUIUtils;
import com.ysg.utils.YConfigUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

public class YChatUtil {
    public interface OnIMListener{
        void onSuccess();
        void onError();
    }

    public interface OnUnReadListener{
        void onUnRead(Long count);
    }

    //======================================

    private static String TAG = "YChatUtil";

    private static YChatUtil instance;

    private YChatUtil(){
    }

    public static YChatUtil getInstance(){
        if (instance == null){
            synchronized (YChatUtil.class){
                if (instance == null){
                    instance = new YChatUtil();
                }
            }
        }
        return instance;
    }

    public void login(String txUserId, String sign, int appId, OnIMListener listener){
        TIMAppService.getInstance().initBeforeLogin(0);//TODO 在闪屏页是否需要这个

//        if (YStringUtil.isEmpty(sign)){
//            sign = GenerateTestUserSig.genTestUserSig(txUserId);
//        }
        UserInfo.getInstance().setUserId(txUserId);
        UserInfo.getInstance().setUserSig(sign);
        TUILogin.login(MyApp.instance(), appId, txUserId, sign, TUIUtils.getLoginConfig(), new TUICallback() {
            @Override
            public void onError(final int code, final String desc) {
                Log.i(TAG, "imLogin errorCode = " + code + ", errorInfo = " + desc);
                if (listener != null){
                    listener.onError();
                }
            }

            @Override
            public void onSuccess() {
                UserInfo.getInstance().setAutoLogin(true);
                UserInfo.getInstance().setDebugLogin(true);
                TIMAppService.getInstance().registerPushManually();

                if (listener != null){
                    listener.onSuccess();
                }
            }
        });
    }

    public void logout(){
        TUILogin.logout(new TUICallback() {
            @Override
            public void onSuccess() {
            }

            @Override
            public void onError(int code, String desc) {
                Log.e("腾讯IM退出", "logout fail: " + code + "=" + desc);
            }
        });
    }

    /**
     * 设置个人资料
     */
    public void setInfo(UserResult user){
        V2TIMUserFullInfo info = new V2TIMUserFullInfo();
        info.setNickname(user.getNick());
        info.setFaceUrl(user.getPic());
        V2TIMManager.getInstance().setSelfInfo(info, new V2TIMCallback() {
            @Override
            public void onSuccess() {
                Log.e("设置个人资料成功", "设置个人资料成功");
            }
            @Override
            public void onError(int code, String desc) {
                Log.e("设置个人资料失败", "code:"+code + "desc"+desc);
            }
        });
    }

    public void initLoginStatusListener() {
        TUILogin.addLoginListener(new TUILoginListener() {
            @Override
            public void onKickedOffline() {
                YToastUtil.showLong(R.string.im_repeat_login_tip);
                YLoginUtil.getInstance().logout(BaseApplication.getContext());
            }

            @Override
            public void onUserSigExpired() {
                YToastUtil.showLong(R.string.im_expired_login_tip);
            }
        });
    }

    public void setFreeType(ChatCallEnum callEnum){
        CallActivity.freeType = callEnum == ChatCallEnum.AUDIO? CallFreeTypeEnum.AUDIO_MATCH.getType(): CallFreeTypeEnum.VIDEO_MATCH.getType();
    }

    public void setUnReadCount(OnUnReadListener listener){
        V2TIMManager.getConversationManager().getTotalUnreadMessageCount(new V2TIMValueCallback<Long>() {
            @Override
            public void onSuccess(Long aLong) {
//                setUnReadCount(aLong);
                if (listener != null) listener.onUnRead(aLong);
            }

            @Override
            public void onError(int code, String desc) {
                Log.e("获取未读信息数量出错：", "code:"+code + " ;desc:"+desc);
            }
        });
        V2TIMManager.getConversationManager().addConversationListener(new V2TIMConversationListener() {
            @Override
            public void onTotalUnreadMessageCountChanged(long totalUnreadCount) {
                super.onTotalUnreadMessageCountChanged(totalUnreadCount);
//                setUnReadCount(totalUnreadCount);
                if (listener != null) listener.onUnRead(totalUnreadCount);
            }
        });
    }


}
